// pages/find/find.js
import {
  mainInList,
  categoryByLevel
} from '../../api/apis.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerConfig: {
      indicatorDots: true, //小点
      autoplay: true, //是否自动轮播
      interval: 5000, //间隔时间
      duration: 800 //滑动时间
    },
    category: '',
    conditions: '',
    tabList: [],
    findList: [],
    pageIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._initData()
  },
  _initData() {
    this._getBanner()
    this._getCategory()
  },
  _getBanner() {
    mainInList({
      data: {
        index: 1,
        size: 5,
        categorys: 146,
        menuID: 7,
        recommendType: 5
      },
      success: res => {
        if (res.state) {
          let banners = []
          res.data.list.forEach((item) => {
            banners.push({
              link: '../detail/detail?did=' + item.id,
              url: item.images.length > 0 ? item.images[0].URL : ""
            })
          })
          this.setData({
            bannerList: banners
          })
        }
      },
      fail: res => {}
    })
  },
  _getCategory() {
    categoryByLevel({
      data: {
        menuID: 7,
        id: 146,
        level: 2
      },
      success: res => {
        if (res.state) {
          res.data.map((item, index) => {
            if (index === 0) {
              item.actived = true
            } else {
              item.actived = false
            }
            return item
          })
          this.setData({
            tabList: res.data,
            category: res.data[0].GroupID
          })
          this._getList()
        }
      }
    })
  },
  //tab页切换
  click: function(e) {
    this.data.pageIndex = 0
    let index = e.currentTarget.dataset.index
    let current = "tabList[" + index + "].actived"
    this.data.tabList.map((item, index) => {
      item.actived = false;
      return item
    })
    this.setData({
      tabList: this.data.tabList,
      [current]: true,
      category: e.currentTarget.dataset.item.GroupID
    })
    this.data.findList = []
    this._getList()
  },
  _getList() {
    this.data.pageIndex++
      mainInList({
        data: {
          index: this.data.pageIndex,
          size: 30,
          categorys: this.data.category,
          menuID: 7,
          conditions: this.data.conditions
        },
        success: res => {
          if (res.state) {
            res.data.list.map((item) => {
              item.img = item.images.length > 0 ? item.images[0].URL : ""
            })
            this.setData({
              findList: this.data.findList.concat(res.data.list)
            })
            if (this.data.findList.length >= res.data.sum) {
              this.setData({
                noData: true
              })
            }
          }
        }
      })
  },
  onItemClick(e) {
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?did=' + e.currentTarget.dataset.item.id,
    })
  },
  setSearchWords(e) {
    this.setData({
      conditions: e.detail.value
    })
  },
  search() {
    this.data.findList = []
    this.data.pageIndex = 0
    this._getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this._initData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(e) {
    this._getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})